const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    list:{},
    type:''
  },

  detail:function(e){
    const that = this;
    const index = e.currentTarget.dataset.index;
    const data = that.data.list;
    wx.navigateTo({
      url: '../systemMessage/systemMessage?id=' + data[index]['id']
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    app.util.request({
      url: 'Index/Index/getDetailsMessgae',
      data: {
        openid: app.globalData.userInfo.openid,
        type: options.type
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        that.setData({
          list:res.data,
          type: options.type
        })
      }
    })
  },
  delMessage:function(e){
    const that = this;
    const index = e.currentTarget.dataset.index;
    const data = that.data.list;
    var id = data[index]['id'];
    app.util.request({
      url: 'Index/Index/delMessage',
      data: {
        openid: app.globalData.userInfo.openid,
        type:'1',
        id: id
      },
      header: {
        'content-type': 'application/json'
      },
      success(res){
        data.splice(index, 1);
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
        that.setData({
          list: data
        })
      }
    })
  },
  delAllMessage:function(){
    const that = this;
    const data = that.data.list;
    var str = '';
    for(var i=0;i<data.length;i++){
      str += data[i]['id']+',';
    }
    str = str.substr(0, str.length - 1);
    app.util.request({
      url: 'Index/Index/delMessage',
      data: {
        openid: app.globalData.userInfo.openid,
        type: '2',
        id: str
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
        that.setData({
          list: {}
        })
      }
    })
  },
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },
  details:function(){
    wx.navigateTo({
      url: '../systemMessage/systemMessage'
    })
  },
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    if (that.data.type !== ''){
      app.util.request({
        url: 'Index/Index/getDetailsMessgae',
        data: {
          openid: app.globalData.userInfo.openid,
          type: that.data.type
        },
        header: {
          'content-type': 'application/json'
        },
        success(res) {
          that.setData({
            list: res.data
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})